var myCodeMirror = {};
var curpDirPath = '';
var control = {
	
	init:function(){
		this.set_all_windos();
		this.listen_quick_button();
		this.explorer_init();
		this.listen_sub_tab();
		this.listen_save_btn();
		this.listen_close_all_btn();
		this.listen_install();
		this.listen_sort_move();
	},
	listen_sort_move:function(){
		$("#sort_up,#sort_down").unbind('click').click(function(){
			if($("#explorer .selected").length==2){
				if($("#sort_move").length>0){}else{
					$("body").append('<div id="sort_move"></div>');
				}
				path = $("#explorer .selected").eq(1).attr("path");
				var str = '<form action="?act=sort_move&t='+$(this).attr('id')+'" target="editbox" id="edit" method="post"><input type="hidden" name="path" value="'+path+'" /></form>';
				$("#sort_move").html(str);
				$("#sort_move form").submit();
				$("#sort_move").html('');
				listreload($("#explorer .selected").eq(0).attr("path").replace('/','_'));
			}
		});
	},
	listen_install:function(){
		$("#install").unbind('click').click(function(){
			if($("#explorer .selected").length==2){
				if($("#install_control").length>0){}else{
					$("body").append('<div id="install_control"></div>');
				}
				path = $("#explorer .selected").eq(1).attr("path");
				var str = '<form action="?act=install" target="editbox" id="edit" method="post"><input type="hidden" name="path" value="'+path+'" /></form>';
				$("#install_control").html(str);
				$("#install_control form").submit();
				$("#install_control").html('');
				listreload($("#explorer .selected").eq(0).attr("path").replace('/','_'));
			}
		});
	},
	listen_new_file_btn:function(){
		$("#new_file").unbind('click').click(function(){
			if($("#explorer .selected").length>0){
				path = $("#explorer .selected").eq(0).attr("path");
				if($("#new_window").length>0){}else{
					var type = '';
					if(path.indexOf('list/')==-1) type = '<select name="type"><option value="text">text</option><option value="select">select</option><option value="radio">radio</option><option value="checkbox">checkbox</option><option value="textarea">textarea</option></select>';
					var str = '<form action="?act=new_file" target="editbox" id="edit" method="post"><input type="text" name="field_name" value="" />'+type+'<input type="hidden" name="path" value="'+path+'" /><input type="submit" value="save" /></form>';
					control.open_new_window(str);
				}
			}
		});
	},
	open_new_window:function(str){
		if($("#new_window_size").length>0){}else{
			$("body").append('<div id="new_window_size"></div>');
		}
		$("#new_window_size").html(str);
		var wstr = '<div id="new_window" style="top:'+($(window).height()/2-$("#new_window_size").height()/2)+'px;left:'+($(window).width()/2-$("#new_window_size").width()/2)+'px;"><div class="title"><div class="close"><img src="skin/image/ico18.gif"></div></div>'+str+'</div>';
		$("body").append(wstr);
		control.listen_close_new_window_btn();
	},
	listen_close_new_window_btn:function(){
		$("#new_window .title .close").unbind('click').click(function(){
			$("#new_window").remove();
		});
	},
	listen_sub_tab:function(){
		$("#sub_tab li").unbind('click').click(function(){
			var type = $(this).attr("id").replace('sub_tab_','');
			if($(".CodeMirror").length>0){
				$(".CodeMirror").each(function(i){
				   if($(this).css('display')!='none'){
					$(this).hide();
					$("#"+$(this).parent().parent().attr("id")+"_"+type).next().show();
				   }
				});
				control.sub_tab_sel(type);
			} 
		});
	},
	listen_save_btn:function(){
		$("#save_file").unbind('click').click(function(){
			if($(this).find('img').attr('src')=='skin/image/ico1.gif'){
				if($("#file_tab li.selected").length>0){
					var id = $("#file_tab li.selected").attr("id");
					var bid = id.replace("_tab","_body");
					$("#"+bid+" form").submit();
					$(this).find('img').attr('src','skin/image/ico2.gif');
					$("#"+id+" img").eq(0).attr("src","skin/image/ico9.gif");
				}
			}
		});
	},
	listen_close_all_btn:function(){
		$("#close_all_file").click(function(){
			if($("#file_tab li").length>0){
				$("#file_tab li").each(function(){
					if(!$(this).hasClass("selected")) control.close_file_tab($(this).attr("id"));
				 });
			}
		});
	},
	window_change:function(){
		$(window).resize(function(){
		   control.set_main_widow();
		});
	},
	set_main_widow:function(){
		$("#main_table").css('height',$(window).height()-10+'px');
	},
	set_all_windos:function(){
		this.set_main_widow();
		$("#top_td").css('height','30px');
		$("#msg_td").css('height','20px');
		var body_width = $("#main_window_td").parent().width();
		$("#explorer_td").css({width:"200px",padding:"5px"});
		this.window_change();
	},
	close_file_tab:function(id){
		$("#"+id).remove();
		$("#"+id.replace('_tab','_body')).remove();
	},
	file_tab:function(path){
		if(path.indexOf('/')>-1){
			var id = path.replace(/\//g,'_').replace(/\./,'_')+'_tab';
		}else{
			var id = path;
		}
		var parr = path.split('/');
		var text = parr[(parr.length-1)];
		if($("#file_tab li").length>0){
			$("#file_tab li").each(function(i){
			   $(this).find('img').eq(1).attr('src','skin/image/ico13.gif');
			});
			$("#file_tab li").removeClass("selected");
		}
		if($("#file_tab li#"+id).length>0){
			$("#file_tab li#"+id).addClass("selected");
			$("#file_tab li#"+id+" img").eq(1).attr('src','skin/image/ico11.gif');
			$(".CodeMirror").hide();
			$("#"+id.replace("_tab","_body")+"_xml").next().show();
			control.sub_tab_sel('xml');
		}else{
			$("#file_tab ul").append('<li class="selected" id="'+id+'"><img src="skin/image/ico9.gif" />'+text+'<img src="skin/image/ico11.gif" /></a>');
		}
		control.listen_new_file_btn();
		$("#file_tab li").unbind('click').click(function(){
			var id = $(this).attr('id');
			var fid = id.replace('_tab','');
			if($("#"+fid).length>0){
				$("#explorer li").removeClass('selected');
				$("#"+fid).addClass('selected');
				var cobj = $("#"+fid);
				while(true){
					var uobj = $(cobj).prev();
					if($(uobj).hasClass("folder")){
						$(uobj).addClass('selected');
						break;
					}else{
						cobj = uobj;
					}
				}
			}
			if($(this).find('img').eq(0).attr('src')=='skin/image/ico10.gif'){
				$("#save_file img").attr('src','skin/image/ico1.gif');
			}else{
				$("#save_file img").attr('src','skin/image/ico2.gif');
			}
			control.file_tab(id);
			control.listen_new_str();
			control.listen_new_file_btn();
		});
		$("#file_tab li img").unbind('click').click(function(){
			if($(this).attr('src')=='skin/image/ico13.gif'||$(this).attr('src')=='skin/image/ico11.gif'){
				control.close_file_tab($(this).parent().attr("id"));
				control.listen_new_str();
				return false;
			}
		});
	},
	explorer_init:function(){
		var str = '	<ul><li path="list" level="1" class="folder">list</li><li path="search" level="1" class="folder">search</li><li path="edit" level="1" class="folder">edit</li></ul>';
		$("#explorer").html(str);
		this.explorer_list();
	},
	explorer_list:function(){
		$("#explorer li").unbind('click').click( function () { 
			var path = $(this).attr('path');
			var level = parseInt($(this).attr('level'));
			var liobj = $(this); 
			var nextli = $(this).next();
			if(level<3){
				if(nextli.attr('level')==(level+1)){
					control.explorer_list_close(level,path);
				}else{
					$.getJSON("?act=get_file_list&path="+path, function(json){
						if(json.length>0){
							var li = '';
							for(var i=0;i<json.length;i++){
								var folder_ico_class = ' class="folder"';
								if(level>1){
									if(json[i].indexOf(',install')>-1){
										json[i] = json[i].replace(',install','');
										folder_ico_class = ' class="left20 install"';
									}else{
										folder_ico_class = ' class="left20"';
									}
								}	
								li = li + '<li'+folder_ico_class+' path="'+path+'/'+json[i]+'" id="'+((path+'/'+json[i]).replace(/\//g,'_').replace('.','_'))+'" level="'+(level+1)+'" style="margin-left:'+(10*level)+'px;">'+json[i]+'</li>';
							}
							liobj.after(li);
						}
						control.explorer_list();
					});
				}
				if(level==2){
					$("#explorer li").removeClass('selected');
					$(this).addClass('selected');
				}
			}else{
				$("#explorer li").removeClass('selected');
				var cobj = $(this);
				while(true){
					var uobj = $(cobj).prev();
					if($(uobj).hasClass("folder")){
						$(uobj).addClass('selected');
						break;
					}else{
						cobj = uobj;
					}
				}
				$("#explorer li.left20").removeClass('selected');
				$(this).addClass('selected');
				control.explorer_edit(liobj.attr('path'));
			}
			control.listen_new_file_btn();
		});
	},
	explorer_list_close:function(level,path){
		while(true){
			var rm = 0;
			var len = $("#explorer li").length;
			for(var m=0;m<len;m++){
				if($("#explorer li").eq(m).attr('path').indexOf(path)>-1&&$("#explorer li").eq(m).attr('level')>=(level+1)){
					$("#explorer li").eq(m).remove();
					rm++;
					break;
				}
			}
			if(rm==0) break;
		}
	},
	sub_tab_sel:function(type){
		$("#sub_tab li").removeClass("sel");
		$("#sub_tab_"+type).addClass("sel");
	},
	set_myCodeMirror:function(id,type,str){
		if(!eval("myCodeMirror."+id)) eval("myCodeMirror."+id+" = {}");
		if(!eval("myCodeMirror."+id+"."+type)) eval("myCodeMirror."+id+"."+type+" = ''");
		$("#"+id+" form").append('<textarea style="display: none;" id="'+id+'_'+type+'" name="'+id+'_'+type+'">'+str+'</textarea>');
		if(type=='xml'||type=='style'){
			var mode = 'mode: "text/html",lineNumbers: true';
		}else if(type=='javascript'){
			var mode = 'mode: "text/javascript",lineNumbers: true';
		}else{
			var mode = 'lineNumbers: true, mode: "application/x-httpd-php", indentUnit: 4, indentWithTabs: true';
		}
		eval('myCodeMirror.'+id+'.'+type+' = CodeMirror.fromTextArea(document.getElementById("'+id+'_'+type+'"), {'+mode+'});');
		eval('myCodeMirror.'+id+'.'+type+'.setSize("100%","'+($(window).height()-100)+'")');
		$("#"+id+"_"+type).next().hide();
	},
	explorer_edit:function(path){
		this.file_tab(path);
		$.getJSON("?act=edit_file&path="+path, function(data){
			var id = path.replace(/\//g,'_').replace(/\./,'_')+'_body';
			if($("#"+id).length>0){$("#"+id+" form").html('');}else{
				$("#file_body #code").append('<div id="'+id+'"><form action="?act=edit_file" target="editbox" id="edit" method="post"></form></div>');
			}
			var dtype = ['xml','before','after','result','style','javascript'];
			for(var i=0;i<dtype.length;i++){
				if(!eval("data."+dtype[i])) eval('data.'+dtype[i]+' = ""');
				var pstr = eval("data."+dtype[i]);
				if(dtype[i]=='before'||dtype[i]=='after'||dtype[i]=='result') pstr = "<?php"+pstr+"\n?>";
				control.set_myCodeMirror(id,dtype[i],pstr);
			}
			$("#"+id+" form").append('<input type="hidden" name="path" value="'+path+'" />');
			$("#"+id+" form").append('<input type="hidden" name="act" value="save_file" />');
			$(".CodeMirror").hide();
			$("#"+id+"_xml").next().show();
			control.sub_tab_sel('xml');
			control.listen_new_str();
			control.listen_save_btn();
			return false;
		});
	},
	listen_new_str:function(){
		$(".CodeMirror").unbind( "keydown" ).keydown(function(){
			var id = $(this).parent().parent().attr("id");
			$("#"+id.replace("_body","_tab")+" img").eq(0).attr("src","skin/image/ico10.gif");
			$("#save_file img").attr("src","skin/image/ico1.gif");
			control.listen_save_btn();
		});
	},
	listen_quick_button:function(){
		this.set_hover_class($("#quick_button div"),'move');
	},
	set_hover_class:function(obj,move){
		$(obj).hover(
		  function () {
			$(this).addClass(move);
		  },
		  function () {
			$(this).removeClass(move);
		  }
		);
	}
	
}

$(function(){
	control.init();
});
function listreload(path){
	$("#"+path).click();
	$("#"+path).click();
}
function closenewwindown(){
	$("#new_window .title .close").click();
}